﻿using ClockLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.Foundation.Collections;

namespace XiaoweiUWP.Services
{
    public class AlarmService
    {
        private static readonly Lazy<AlarmService> LazyInstance = new Lazy<AlarmService>(() =>
        {
            return new AlarmService();
        }, LazyThreadSafetyMode.ExecutionAndPublication);

        public static AlarmService Instance { get { return LazyInstance.Value; } }

        public delegate void AlarmResetUIHandle();
        public event AlarmResetUIHandle ResetUI;
        private AlarmService()
        {
            ClockManager.Instance.ClockArrived += ClockList_ClockArrvied;
            ClockManager.Instance.ClockListChanged += Instance_ClockListChanged;
        }

        private void Instance_ClockListChanged(object sender, bool e)
        {
            ResetUI?.Invoke();
        }

        public event Action<Clock> ClockArrived;


        private void ClockList_ClockArrvied(object sender, IList<Clock> clock)
        {
            string events = "";
            foreach (var item in clock)
            {
                ClockArrived?.Invoke(item);
                if (!string.IsNullOrEmpty(item.ClockData.Clock_event))
                {
                    events += item.ClockData.Clock_event + "\r\n";
                }
            }
            if (events.Length > 2)
            {
                events = events.Remove(events.Length - 2);
            }
            //ClockManager.Instance.RemoveToastFromHistory(clock[0].ClockData.Trig_time);

        }
        public void Begin()
        {
            ClockManager.Instance.RemoveAll();
        }
    }
}
